package lab03.test03;

import java.io.*;

public class Test03 {
    public static void main(String[] args) {
        String path = "D:\\java实验\\test.txt";

        FileOutputStream fileOutputStream = null;
        FileInputStream fileInputStream = null;
        FileWriter fileWriter = null;

        try {
            try {
                fileInputStream = new FileInputStream(path);
                fileOutputStream = new FileOutputStream(path);

                //在某目录下创建一个文本文件test.txt，通过字节输出流向文件中写入“Beijing欢迎你！”
                String line = "Beijing欢迎你！";
                byte[] bytes = line.getBytes();
                fileOutputStream.write(bytes);

                //通过字节输入流读出文件中的内容并显示
                int data = 0;
                while ((data = fileInputStream.read(bytes)) != -1) {
                    String string = new String(bytes);
                    System.out.println(string);
                }

                //如果在已有文件中再追加“我的学号是：你自己的学号”
                fileWriter = new FileWriter("D:\\java实验\\test.txt", true);
                fileWriter.write("我的学号是：1606101119");

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } finally {
            try {
                fileInputStream.close();
                fileOutputStream.close();
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
